import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';
import '../model/reminder.dart';

class ReminderDialog extends StatefulWidget {
  final String babyId;
  final Reminder? reminder;

  const ReminderDialog({Key? key, required this.babyId, this.reminder})
    : super(key: key);

  @override
  _ReminderDialogState createState() => _ReminderDialogState();
}

class _ReminderDialogState extends State<ReminderDialog> {
  late TextEditingController _titleController;
  late TextEditingController _notesController;
  late ReminderType _selectedType;
  late TimeOfDay _selectedTime;
  late bool _repeat;

  @override
  void initState() {
    super.initState();
    _titleController = TextEditingController(text: widget.reminder?.title);
    _notesController = TextEditingController(text: widget.reminder?.notes);
    _selectedType = widget.reminder?.type ?? ReminderType.feeding;
    _selectedTime = widget.reminder?.scheduleTime ?? TimeOfDay.now();
    _repeat = widget.reminder?.repeat ?? false;
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(widget.reminder == null ? '添加提醒' : '编辑提醒'),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            DropdownButtonFormField<ReminderType>(
              value: _selectedType,
              decoration: const InputDecoration(labelText: '提醒类型'),
              items:
                  ReminderType.values.map((type) {
                    return DropdownMenuItem(
                      value: type,
                      child: Row(
                        children: [
                          Icon(type.icon),
                          const SizedBox(width: 8),
                          Text(type.label),
                        ],
                      ),
                    );
                  }).toList(),
              onChanged: (value) {
                setState(() => _selectedType = value!);
              },
            ),
            TextField(
              controller: _titleController,
              decoration: const InputDecoration(labelText: '标题'),
            ),
            const SizedBox(height: 16),
            ListTile(
              contentPadding: EdgeInsets.zero,
              title: const Text('提醒时间'),
              trailing: Text(_selectedTime.format(context)),
              onTap: () async {
                final time = await showTimePicker(
                  context: context,
                  initialTime: _selectedTime,
                );
                if (time != null) {
                  setState(() => _selectedTime = time);
                }
              },
            ),
            SwitchListTile(
              contentPadding: EdgeInsets.zero,
              title: const Text('每天重复'),
              value: _repeat,
              onChanged: (value) {
                setState(() => _repeat = value);
              },
            ),
            TextField(
              controller: _notesController,
              decoration: const InputDecoration(labelText: '备注'),
              maxLines: 3,
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('取消'),
        ),
        TextButton(onPressed: _save, child: const Text('保存')),
      ],
    );
  }

  void _save() {
    if (_titleController.text.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请输入标题')));
      return;
    }

    final reminder = Reminder(
      id: widget.reminder?.id ?? Uuid().v4(),
      babyId: widget.babyId,
      title: _titleController.text,
      type: _selectedType,
      scheduleTime: _selectedTime,
      repeat: _repeat,
      notes: _notesController.text,
    );

    Navigator.pop(context, reminder);
  }

  @override
  void dispose() {
    _titleController.dispose();
    _notesController.dispose();
    super.dispose();
  }
}
